<html>
<head><meta charset="utf-8"><title>Unsafety of unaligned ptrs · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html">Unsafety of unaligned ptrs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209410764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209410764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209410764">(Sep 08 2020 at 16:25)</a>:</h4>
<p>If the pointer is never read from/written, it is safe, correct? Would it be safe to construct a slice with it as long as it is never read, only length checked?</p>



<a name="209411056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411056">(Sep 08 2020 at 16:27)</a>:</h4>
<p>I don't think this is safe; pointers are always aligned for their type as part of the validity invariant. It's as bad as <code>3 as bool</code></p>



<a name="209411320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411320">(Sep 08 2020 at 16:29)</a>:</h4>
<p>You are making a distinction between pointers and references, correct <span class="user-mention" data-user-id="277614">@Braden Nelson</span>? And by "slice", do you mean <code>&amp;[T]</code> or <code>*const []</code>?</p>



<a name="209411334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411334">(Sep 08 2020 at 16:30)</a>:</h4>
<p>References must always be aligned. Pointers can be unaligned for as long as they are not dereferenced.</p>



<a name="209411483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411483">(Sep 08 2020 at 16:30)</a>:</h4>
<p>an &amp;[T], constructed with from_raw_parts.</p>



<a name="209411493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411493">(Sep 08 2020 at 16:30)</a>:</h4>
<p>Oh, I stand corrected</p>



<a name="209411540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411540">(Sep 08 2020 at 16:31)</a>:</h4>
<p>In that case *const [T] would be okay but not &amp;[T]</p>



<a name="209411545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411545">(Sep 08 2020 at 16:31)</a>:</h4>
<p>I believe creating a <code>&amp;[T]</code> from an unaligned raw pointer is invalid.</p>



<a name="209411563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411563">(Sep 08 2020 at 16:31)</a>:</h4>
<p>the resulting &amp;[T] is never read, only length checked. I'll assume unsafe/invalid.</p>



<a name="209411579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411579">(Sep 08 2020 at 16:31)</a>:</h4>
<p>still UB</p>



<a name="209411615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411615">(Sep 08 2020 at 16:32)</a>:</h4>
<p>References are marked as <code>dereferencable</code> in the generated LLVM ir. This means that LLVM is allowed to dereference it at any point (for example for speculative loading), which requires the reference to be aligned.</p>



<a name="209411669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411669">(Sep 08 2020 at 16:32)</a>:</h4>
<p>Yea, I didn't clarify my response. I understand it as UB. Got it.</p>



<a name="209411751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411751">(Sep 08 2020 at 16:33)</a>:</h4>
<p>I would suggest taking the length directly off the <code>*const [T]</code> if you can</p>



<a name="209411934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411934">(Sep 08 2020 at 16:34)</a>:</h4>
<p>And, since you are calling <code>from_raw_parts</code>, you already have the length, so no real reason to jump through that hoop. :-)</p>



<a name="209411984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411984">(Sep 08 2020 at 16:35)</a>:</h4>
<p>Looking at some rustc code that was using align_to to figure out a safe offset for a fast path. The code is safe (it's working with an &amp;[u8], no alignment requirements), so i'm now trying to extract the important part of align_to I need (just the alignment calculation itself) to remove an unsafe block.</p>



<a name="209411987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209411987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209411987">(Sep 08 2020 at 16:35)</a>:</h4>
<p>I don't know how you'd go from a pointer + length directly to <code>*const [T]</code> though.</p>



<a name="209412068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412068">(Sep 08 2020 at 16:35)</a>:</h4>
<p><a href="https://doc.rust-lang.org/std/ptr/fn.slice_from_raw_parts.html">https://doc.rust-lang.org/std/ptr/fn.slice_from_raw_parts.html</a></p>



<a name="209412170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412170">(Sep 08 2020 at 16:36)</a>:</h4>
<p>Ah.</p>
<blockquote>
<p>1.42.0</p>
</blockquote>



<a name="209412209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412209">(Sep 08 2020 at 16:37)</a>:</h4>
<p>I asked as it would've made it easier to just comment it with // SAFETY as it's been needing, but it's arguably better to just make it not unsafe at all.</p>



<a name="209412248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412248">(Sep 08 2020 at 16:37)</a>:</h4>
<p>As an example for what can go wrong if you turn an unaligned pointer into a reference, even if you don't dereference it: Rust is allowed to make assumptions about valid pointer values and use the other pieces of a reference as a niche. For instance, it could assume non-NULL (which is why you can't have a NULL reference), and it <em>could</em> (though currently it doesn't) assume an aligned pointer and use the low bits as a niche.</p>



<a name="209412269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412269">(Sep 08 2020 at 16:37)</a>:</h4>
<p>The unsafety is used solely for a normally safe calculation.</p>



<a name="209412373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412373">(Sep 08 2020 at 16:38)</a>:</h4>
<p>As a separate topic, you might want to post the concrete code. You've got experienced people looking at it, so you might be able to get some interesting solutions <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="209412598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209412598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209412598">(Sep 08 2020 at 16:40)</a>:</h4>
<p>Original code. I'm transforming this line into smth slightly longer, but no unsafe. <a href="https://github.com/rust-lang/rust/blob/7bc0bf72545f5d6ac6d9639391da875d5e147d9a/library/core/src/slice/memchr.rs#L100">https://github.com/rust-lang/rust/blob/7bc0bf72545f5d6ac6d9639391da875d5e147d9a/library/core/src/slice/memchr.rs#L100</a><br>
Giving the file proper <code>// SAFETY</code> docs in the same change.</p>



<a name="209413272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413272">(Sep 08 2020 at 16:46)</a>:</h4>
<p>Why not just adapt the <a href="https://doc.rust-lang.org/src/core/slice/mod.rs.html#2576-2602">source of align_to</a> to just get the arithmetic stuff, which is safe anyway on your types?</p>



<a name="209413334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413334">(Sep 08 2020 at 16:47)</a>:</h4>
<p>Effectively what I'm doing. Original question was in the context of keeping the unsafe {}, but I've realized I can get rid of it entirely.</p>



<a name="209413406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413406">(Sep 08 2020 at 16:47)</a>:</h4>
<p>looks like the core function is <code>core::ptr::align_offset</code>, but that is also unsafe</p>



<a name="209413496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413496">(Sep 08 2020 at 16:48)</a>:</h4>
<p>wait no it isn't</p>



<a name="209413529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413529">(Sep 08 2020 at 16:48)</a>:</h4>
<p>The power-of-two checked version is perfectly safe.</p>



<a name="209413633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413633">(Sep 08 2020 at 16:49)</a>:</h4>
<p>Why is this line in an unsafe block?</p>
<div class="codehilite"><pre><span></span><code><span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">offset</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">crate</span>::<span class="n">ptr</span>::<span class="n">align_offset</span><span class="p">(</span><span class="n">ptr</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">())</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="209413782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413782">(Sep 08 2020 at 16:50)</a>:</h4>
<p>I honestly have no idea.</p>



<a name="209413919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209413919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209413919">(Sep 08 2020 at 16:51)</a>:</h4>
<p>Oh I see, the <code>align_offset</code> method calls a private unsafe free function also called <code>align_offset</code> which is not power of 2 checked</p>



<a name="209414085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209414085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209414085">(Sep 08 2020 at 16:52)</a>:</h4>
<p>I had occasion to look at the code of <code>align_offset</code> the other day and I have to say it's about 2 orders of magnitude larger than I expected to see</p>



<a name="209414190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209414190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209414190">(Sep 08 2020 at 16:53)</a>:</h4>
<p><a href="https://doc.rust-lang.org/src/core/ptr/mod.rs.html#1168-1271">https://doc.rust-lang.org/src/core/ptr/mod.rs.html#1168-1271</a></p>



<a name="209422715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209422715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209422715">(Sep 08 2020 at 18:04)</a>:</h4>
<p>What might be a good way to assert a pointer is aligned correctly?</p>



<a name="209422806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209422806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209422806">(Sep 08 2020 at 18:04)</a>:</h4>
<p><code>p.align_to(n) == p</code>?</p>



<a name="209422826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209422826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209422826">(Sep 08 2020 at 18:05)</a>:</h4>
<p>That should work. Thank you.</p>



<a name="209422855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209422855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209422855">(Sep 08 2020 at 18:05)</a>:</h4>
<p>that boils down to <code>p &amp; (n-1) == 0</code> if you want something faster</p>



<a name="209422864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209422864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209422864">(Sep 08 2020 at 18:05)</a>:</h4>
<p>Hopefully the compiler can figure that out.</p>



<a name="209422897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209422897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209422897">(Sep 08 2020 at 18:05)</a>:</h4>
<p>or <code>p.align_offset(n) == 0</code></p>



<a name="209423039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423039">(Sep 08 2020 at 18:07)</a>:</h4>
<p>oh, maybe that doesn't work because it uses units of <code>T</code></p>



<a name="209423043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423043">(Sep 08 2020 at 18:07)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> Ah, that's even better.</p>



<a name="209423053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423053">(Sep 08 2020 at 18:07)</a>:</h4>
<p>Oh, hrm.</p>



<a name="209423163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423163">(Sep 08 2020 at 18:08)</a>:</h4>
<p>It'd work if you cast the pointer to <code>u8</code> first, but that's a pain.</p>



<a name="209423214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423214">(Sep 08 2020 at 18:08)</a>:</h4>
<p>is pointer <code>align_to</code> really a thing? I only see slice and layout methods</p>



<a name="209423379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Braden Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423379">(Sep 08 2020 at 18:10)</a>:</h4>
<p>Doesn't seem to be a thing. Hrm. I need to get some other person(s) to look over the changes anyways, as it's a change to memrchr, so i'll PR now, having run the test suite.</p>



<a name="209423401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423401">(Sep 08 2020 at 18:10)</a>:</h4>
<p>One moment...</p>



<a name="209423648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209423648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209423648">(Sep 08 2020 at 18:12)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">is_aligned</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Sized</span><span class="o">&gt;</span><span class="p">(</span><span class="n">ptr</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="n">ptr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="p">(</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">is_aligned</span><span class="p">(</span><span class="mi">5</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">),</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">is_aligned</span><span class="p">(</span><span class="mi">5</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u16</span><span class="p">),</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">is_aligned</span><span class="p">(</span><span class="mi">6</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u16</span><span class="p">),</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">is_aligned</span><span class="p">(</span><span class="mi">6</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u32</span><span class="p">),</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">is_aligned</span><span class="p">(</span><span class="mi">8</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u32</span><span class="p">),</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209424069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209424069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209424069">(Sep 08 2020 at 18:16)</a>:</h4>
<p>could use <code>mem::align_of_val_raw</code> for <code>T: ?Sized</code></p>



<a name="209424173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209424173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209424173">(Sep 08 2020 at 18:17)</a>:</h4>
<p>hmm, well maybe the safety there is too questionable</p>



<a name="209424197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209424197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209424197">(Sep 08 2020 at 18:17)</a>:</h4>
<p>That's also nightly-only.</p>



<a name="209424400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209424400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209424400">(Sep 08 2020 at 18:19)</a>:</h4>
<p>Another approach, if you prefer method syntax:</p>
<div class="codehilite"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">IsAlignedExt</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">is_aligned</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="n">IsAlignedExt</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">is_aligned</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">((</span><span class="o">*</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="p">(</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Sized</span><span class="o">&gt;</span><span class="w"> </span><span class="n">IsAlignedExt</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">is_aligned</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">((</span><span class="o">*</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="p">(</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">((</span><span class="mi">5</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u8</span><span class="p">).</span><span class="n">is_aligned</span><span class="p">(),</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">((</span><span class="mi">5</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u16</span><span class="p">).</span><span class="n">is_aligned</span><span class="p">(),</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">((</span><span class="mi">6</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u16</span><span class="p">).</span><span class="n">is_aligned</span><span class="p">(),</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">((</span><span class="mi">6</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u32</span><span class="p">).</span><span class="n">is_aligned</span><span class="p">(),</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">((</span><span class="mi">8</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">u32</span><span class="p">).</span><span class="n">is_aligned</span><span class="p">(),</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209452916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209452916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209452916">(Sep 08 2020 at 22:41)</a>:</h4>
<p>Is there a way to reasonably construct an instance of a DST, like <code>struct S&lt;T&gt; { t: T, bytes: [u8] }</code> from a <code>T</code>? The last I checked, trying to construct this required basically guessing the layout of DST's?</p>



<a name="209453071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209453071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209453071">(Sep 08 2020 at 22:43)</a>:</h4>
<p>you need indirection like <code>Box</code>/<code>Rc</code>/<code>Arc</code> for a DST instance</p>



<a name="209453118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209453118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209453118">(Sep 08 2020 at 22:44)</a>:</h4>
<p>I've seen <a href="https://crates.io/crates/slice-dst"><code>slice-dst</code></a>, but haven't used it yet</p>



<a name="209453290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209453290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209453290">(Sep 08 2020 at 22:46)</a>:</h4>
<p>Well, yes, in my case, I'm fine just calling malloc and getting bytes for the indirection. But I still need to turn those bytes into a <code>*const S&lt;T&gt;</code> somehow.</p>



<a name="209538689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209538689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209538689">(Sep 09 2020 at 17:01)</a>:</h4>
<p>Shouldn't you be able to use Layout::new + .extend (the DST) + pad_to_align to safely get the layout of a <code>repr(C)</code> DST?</p>



<a name="209539501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209539501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209539501">(Sep 09 2020 at 17:07)</a>:</h4>
<p>Oh, I see what you mean. We never adapted any RFC to explicitly set the metadata of a fat pointer right? So you either need to pretend it's a array of <code>u8</code> or you need to guess the layout of fat pointers...</p>



<a name="209580701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209580701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209580701">(Sep 09 2020 at 23:22)</a>:</h4>
<p>Yeah, the layout of fat pointers is apparently, a pointer to the start and the length of the slice at the end, in elements (not bytes). <code>slice-dst</code> doesn't guess the layout of fat pointers, it just creates a slice using <code>slice_from_raw_parts</code> and casts it to the right type.</p>



<a name="209645497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsafety%20of%20unaligned%20ptrs/near/209645497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsafety.20of.20unaligned.20ptrs.html#209645497">(Sep 10 2020 at 14:00)</a>:</h4>
<p>Sure, the thing is a while ago (like over a year maybe two) there was some discussions maybe a RFC about being able to more explicitly handle the fat pointer metadata which was rejected/postpone as many other things had higher priority back then as far as I remember. <code>slice-dst</code> is definitely worth to look at  if you want to do DSTs it's not supper simple but e.g. show you how to properly do thinks like handling panics while populating a slice DST from a iterator (assuming they do it correctly which seems the way but I didn't look to closely at it).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>